unit FCursor;

interface
uses
	Windows, ExtCtrls, FOpenGl, FTextures, FLogs, SysUtils;

type
	TMouse = Record
		X : Extended;
		Y : Extended;
  	Cursor : Integer;
		CursorX : Integer;
		CursorY : Integer;
		Left : Boolean;
		Right : Boolean;
		Up : Boolean;
		Down : Boolean;
		Hover : Integer;
	end;

const
	CU_NORMAL = 4;
	CU_TARGET = 2;
	CU_NO_TARGET = 3;
	CU_ARROW = 6;

	CursorSize = 32;
	CursorPerRow = 8;
	CursorPerCol = 4;

var
	Mouse : TMouse;
                                
procedure InitCursor(panel : TPanel);
procedure DisplayCursor();
procedure SetCursor( cursor : integer; left : boolean = false; right : boolean = false; up : boolean = false; down : boolean = false );

implementation

procedure InitCursor(panel : TPanel);
begin
	ShowCursor(False);
	panel.Cursor := -1;
	SetCursor(CU_NORMAL);
end;

// Use false as default value !
procedure SetCursor( cursor : integer; left : boolean = false; right : boolean = false; up : boolean = false; down : boolean = false );
begin
	Mouse.Cursor := cursor;

	Mouse.CursorX := cursor mod CursorPerRow;
	Mouse.CursorY := cursor div CursorPerRow;

	if (left or right or up or down) then begin
		Mouse.Left := Mouse.Left or left;
		Mouse.Right := Mouse.Right or right;
		Mouse.Up := Mouse.Up or up;
		Mouse.Down := Mouse.Down or down;
	end else begin
		Mouse.Left := false;
		Mouse.Right := false;
		Mouse.Up := false;
		Mouse.Down := false;
  end;
end;

procedure DisplayCursor();
var
	position : array[0..3] of array[0..3] of extended;
	angle : integer;
	i : integer;
begin
	if Mouse.Up and Mouse.Left then
		angle := -135
	else if Mouse.Up and Mouse.Right then
		angle := -45
	else if Mouse.Up and not(Mouse.Left or Mouse.Right) then
		angle := -90
	else if Mouse.Down and Mouse.Left then
		angle := 135
	else if Mouse.Down and Mouse.Right then
		angle := 45
	else if Mouse.Down and not(Mouse.Left or Mouse.Right) then
		angle := 90
	else if Mouse.Left and not(Mouse.Down or Mouse.Up) then
		angle := 180
	else //if Mouse.Right and not(Mouse.Down and Mouse.Up) then
  	angle := 0;

	position[0][0] := (Mouse.CursorX - 1) / CursorPerRow;
	position[0][1] := (Mouse.CursorY - 1) / CursorPerCol;
	position[0][2] := 0;
	position[0][3] := 0;

	position[1][0] := Mouse.CursorX / CursorPerRow;
	position[1][1] := (Mouse.CursorY - 1) / CursorPerCol;
	position[1][2] := CursorSize;
	position[1][3] := 0;

	position[2][0] := (Mouse.CursorX - 1) / CursorPerRow;
	position[2][1] := Mouse.CursorY / CursorPerCol;
	position[2][2] := 0;
	position[2][3] := CursorSize;

	position[3][0] := Mouse.CursorX / CursorPerRow;
	position[3][1] := Mouse.CursorY / CursorPerCol;
	position[3][2] := CursorSize;
	position[3][3] := CursorSize;

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	BindTexture('Textures\Cursor\HumanCursor.tga');

	glPushMatrix();
	glTranslatef(Mouse.X, Mouse.Y, 0);
	if Mouse.Cursor = CU_ARROW then begin
		glRotatef(angle, 0, 0, 1);
		glTranslatef(-CursorSize, -CursorSize/2, 0);
	end;
	
	glBegin(GL_QUAD_STRIP);
	for i := 0 to 3 do begin
		glTexCoord2f(position[i][0], position[i][1]);
		glVertex3f(  position[i][2], position[i][3], 1);
	end;
	glEnd();
	glPopMatrix();

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
end;



end.

